package play.modules.mongo;

import java.util.LinkedHashMap;
import java.util.Map;

import org.bson.types.ObjectId;


/**
 * This class provides the abstract declarations for all MongoModels.
 * Implementations of these declarations are provided by the MongoEnhancer.
 * 
 * @author Andrew Louth
 */
public class MongoModel {
	
	private Map<String, Object> properties;
	
	public MongoModel(){
		properties =  new LinkedHashMap<String, Object>();
	}
	
	protected Boolean has(String key){
		return properties.containsKey(key);
	}
	
	protected <T extends Object> T get(String key){
		return (T) properties.get(key);
	}
	
	protected void set(String key, Object value){
		if(value == null){
			properties.remove(key);
		}else{
			properties.put(key, value);
		}
	}
	
	public <T extends MongoModel> T init(Map data) {
		this.properties.putAll(data);
		return (T) this;
	}
	
	public Map map() {
		return properties;
	}
	
	public String getId() {
		if(!has("_id")){
			set("_id", ObjectId.get().toString());
		}
		return (String)get("_id");
	}
    
    public void setId(String value) {
        set("_id",value); 
    }
	
    public static String getCollectionName() {
        throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }

    public static long count() {
        throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public static long count(String query, Object... params) {
        throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public static MongoCursor find(String query, Object... params) {
        throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public static MongoCursor find() {
        throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public <T extends MongoModel> T save() {
        throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public void delete() {
        throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public static long delete(String query, Object... params) {
        throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public static long deleteAll(){
    	throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public static void index(String indexString){
    	throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public static void dropIndex(String indexString){
    	throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public static void dropIndexes(){
    	throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
    public static String[] getIndexes(){
    	throw new UnsupportedOperationException("Please annotate your model with @MongoEntity annotation.");
    }
    
}
